package android.cst.hqu.edu.cn.basemvp.mvp.Model;

import android.cst.hqu.edu.cn.basemvp.mvp.Presenter.LoginContract;
import android.cst.hqu.edu.cn.basemvp.mvp.VO.ApiResponse;
import android.cst.hqu.edu.cn.basemvp.mvp.VO.User;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class UserModel implements LoginContract.IModel {
    @Override
    public void login(User user, final LoginContract.IModel.LoginCallback loginCallback) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        LoginService loginService = retrofit.create(LoginService.class);
        loginService.login(user.getName(), user.getPassword())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<ApiResponse>() {
                    @Override
                    public void onNext(ApiResponse apiResponse) {
                        loginCallback.onResponse(apiResponse);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
    private interface LoginService {
        @GET(API.LOGIN_URL)
        Observable<ApiResponse> login(@Query("mobile")String username, @Query("password")String password);
    }
}
